#region Usings

using System;
using System.Windows.Forms;
using Match.Foundation.Business.Classes;
using Match.Foundation.Enums;
using Match.TimingApplication.Classes;

#endregion

namespace Match.TimingApplication.Forms
{
    public partial class frmRaceStart : Form
    {
        #region Private Members
        private TransferManager _TransferManager;
        #endregion

        #region Constructor

        public frmRaceStart(TransferManager TransferManager)
        {
            InitializeComponent();
            _TransferManager = TransferManager;
            RefreshListViewRace();
        }

        #endregion

        #region Form Events
        private void timerFormRefresh_Tick(object sender, EventArgs e)
        {
            lblDate.Text = DateTime.Now.ToLongDateString();
            lblTime.Text = DateTime.Now.ToLongTimeString() + "." + DateTime.Now.Millisecond.ToString("000");
        }
        private void frmRaceStart_FormClosed(object sender, FormClosedEventArgs e)
        {
            Dispose();
        }
        private void buttonClose_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void buttonStartRace_Click(object sender, EventArgs e)
        {
            DateTime StartTime = DateTime.Now;
            foreach (Int32 Index in listViewRace.CheckedIndices)
            {
                RaceInfo Info = (RaceInfo) listViewRace.Items[Index].Tag;
                listViewRace.Items[Index].Checked = false;
                _TransferManager.RegisterRaceStart(new RaceTimeItem(Info.Name, Info.ID, StartTime));
            }
            RefreshListViewRace();
        }
        #endregion

        #region Private Methods
        private void RefreshListViewRace()
        {
            listViewRace.Items.Clear();
            foreach (RaceInfo Info in _TransferManager.GetRaces())
            {
                if (Info.Status == RaceState.Preparing)
                {
                    listViewRace.Items.Add(
                        new ListViewItem(new String[] {Info.Name, Info.StarterCount.ToString(), Info.Status.ToString()}))
                        .Tag = Info;
                }
            }
        }
        #endregion
    }
}